﻿using H.Cmd;
using H.PK;
using System;
using System.IO;

namespace H.PackManager
{
    internal class Program
    {
        private static void DisplayHelper()
        {
            Console.WriteLine("Usage: <exe> /install <sourceFile>");
            Console.WriteLine("Usage: <exe> /list");
        }


        public static int Main(string[] args)
        {
            Arguments arguments = new Arguments(args);
            if (arguments.Count == 0)
            {
                DisplayHelper();
                return 0;
            }

            switch (arguments[0])
            {
                case "/install":
                    {
                        foreach (string pluginFile in arguments.GetOptions(1))
                        {
                            if (File.Exists(pluginFile))
                            {
                                string pluginDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "plugin");
                                HPackInfo packInfo = HPackInfo.CreateInstance(File.ReadAllBytes(pluginFile));
                                HPackRecord record = new HPackRecord(Path.Combine(pluginDir, HPackRecord.IndexFileName));
                                string fackName = Path.Combine(pluginDir, record.NewPack(packInfo.PackageName) + ".hpk");
                                if (File.Exists(fackName))
                                {
                                    packInfo.Write(fackName);
                                    Console.WriteLine("已更新安装: " + pluginFile);
                                }
                                else
                                {
                                    packInfo.Write(fackName);
                                    Console.WriteLine("已全新安装: " + pluginFile);
                                }
                            }
                            else
                            {
                                Console.Error.WriteLine("文件不存在: " + pluginFile);
                            }
                        }
                        return 0;
                    }
                case "/list":
                    {
                        string pluginDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "plugin");
                        HPackRecord record = new HPackRecord(Path.Combine(pluginDir, HPackRecord.IndexFileName));

                        int i = 1;
                        foreach (System.Collections.Generic.KeyValuePair<string, string> kv in record.Installed)
                        {
                            if (File.Exists(Path.Combine(pluginDir, kv.Value + ".hpk")))
                            {
                                Console.WriteLine($"{i,2} | {kv.Key}");
                            }
                            else
                            {
                                Console.WriteLine($"{i,2} | {kv.Key} (NOT FOUND)");
                            }
                            i++;
                        }
                        return 0;
                    }
                default:
                    {
                        Console.Error.WriteLine("未知命令");
                        return 1;
                    }
            }
        }

    }
}
